#include <iostream>
#include <type_traits>

struct MyStruct {
    char c;
    int i;
    double d;
};

int main() {
    // 检查基本数据类型的对齐要求
    std::cout << "Alignment of int: " << std::alignment_of_v<int> << " bytes\n";
    std::cout << "Alignment of double: " << std::alignment_of_v<double> << " bytes\n";

    // 检查自定义结构的对齐要求
    std::cout << "Alignment of MyStruct: " << std::alignment_of_v<MyStruct> << " bytes\n";

    // 使用 std::alignment_of_v 进行内存对齐的动态分配
    void* ptr = aligned_alloc(std::alignment_of_v<MyStruct>, sizeof(MyStruct));
    if (ptr) {
        MyStruct* myStruct = new(ptr) MyStruct; // 使用定位new构造对象
        // 使用 myStruct 做一些操作
        myStruct->c = 'a';
        myStruct->i = 123;
        myStruct->d = 3.14159;

        std::cout << "MyStruct instance - char: " << myStruct->c
                  << ", int: " << myStruct->i
                  << ", double: " << myStruct->d << std::endl;

        myStruct->~MyStruct(); // 手动调用析构函数
        free(ptr); // 释放分配的内存
    }

    return 0;
}